package com.m3839.sdk.anti.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.m3839.sdk.anti.constant.AntiConstant;
import com.m3839.sdk.common.base.BaseBean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:ButtonBean
 *
 * @author 陈宝阳
 * @create 2020/1/2 14: 36
 */
public class ButtonBean extends BaseBean<ButtonBean> implements Parcelable {

  public String name;
  public String func;
  public String url;
  public String target;

  public ButtonBean() {}

  public ButtonBean(JSONObject jsonObject){
    parseBean(jsonObject);
  }

  public ButtonBean(String name, String func) {
    this(name, func, "", "");
  }

  public ButtonBean(String name, String func, String url, String target) {
    this.name = name;
    this.func = func;
    this.url = url;
    this.target = target;
  }

  protected ButtonBean(Parcel in) {
    name = in.readString();
    func = in.readString();
    url = in.readString();
    target = in.readString();
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeString(func);
    dest.writeString(url);
    dest.writeString(target);
  }

  public static final Creator<ButtonBean> CREATOR = new Creator<ButtonBean>() {
    @Override
    public ButtonBean createFromParcel(Parcel in) {
      return new ButtonBean(in);
    }

    @Override
    public ButtonBean[] newArray(int size) {
      return new ButtonBean[size];
    }
  };

  @Override
  public String toString() {
    return "ButtonBean{" +
        "name='" + name + '\'' +
        ", func='" + func + '\'' +
        ", url='" + url + '\'' +
        ", target='" + target + '\'' +
        '}';
  }

  @Override
  public ButtonBean parseBean(JSONObject jsonObject) {
    if(jsonObject == null) {
      return null;
    }

    name = jsonObject.optString(AntiConstant.KEY_NAME);
    func = jsonObject.optString(AntiConstant.KEY_FUNC);
    JSONObject urlJson = jsonObject.optJSONObject(AntiConstant.KEY_URL_INFO);
    if(urlJson != null) {
      url = urlJson.optString(AntiConstant.KEY_URL);
      target = urlJson.optString(AntiConstant.KEY_TARGET);
    }
    return this;
  }


}
